import { message, Button, Modal } from 'antd';
const { confirm } = Modal;

const currentYear = new Date().getFullYear();
const promisify = (func, opts) => {
  return new Promise((resolve, reject) => {
    let entity = func({
      ...opts,
      onOk: close => {
        resolve();
        close();
      },
    });
  });
};
export default {
  namespace: 'base',
  state: {
    dict: {
      approveStatus: [
        {
          label: '待审核',
          value: 'PENDING',
        },
        {
          label: '审核中',
          value: 'GOING',
        },
        {
          label: '已通过',
          value: 'PASS',
        },
        {
          label: '不通过',
          value: 'REFUSE',
        },
      ],
      projectStatus: [
        {
          label: '储备中',
          value: 'STORE',
        },
        {
          label: '规划中',
          value: 'PLAN',
        },
        {
          label: '建设中',
          value: 'BUILD',
        },
        {
          label: '使用中',
          value: 'USE',
        },
        {
          label: '已关闭',
          value: 'CLOSE',
        },
      ],
      buildType: [
        {
          label: '建设',
          value: 'BUILD',
        },
        {
          label: '保障',
          value: 'PURCHASE_SERVICE',
        },
      ],
      budget: [
        {
          label: '0~50万',
          value: '0~50',
        },
        {
          label: '50~400万',
          value: '50~400',
        },
        {
          label: '400~1000万',
          value: '400~1000',
        },
        {
          label: '1000万以上',
          value: '1000+',
        },
      ],
      yearList: [0, 1].map(item => ({
        label: `${currentYear + item}年`,
        value: currentYear + item,
      })),
      fiveYears: [0, 1, 2, 3, 4].map(item => ({
        label: `${currentYear + item}年`,
        value: currentYear + item,
      })),
      searchYear: [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4].map(item => ({
        label: `${currentYear + item}年`,
        value: currentYear + item,
      })),
    }, //常量数据字典
    approve: { plan: true, year: true, nextYear: true },
  },
  reducers: {
    save(state, { payload }) {
      let newValue = Object.assign({}, state, payload);
      return newValue;
    },
  },
  effects: {
    *delete({ payload }, { put, call, select }) {
      const { url, data, type } = payload;
      yield promisify(confirm, {
        content: `您确定要删除${type}吗？`,
        okText: '确认',
        cancelText: '取消',
      });

      yield put.resolve({
        type: '$request/post',
        payload: { url, data },
      });

      yield put({
        type: '$msg/showToast',
        payload: {
          title: '删除成功！',
          type: 'success',
        },
      });
    },
    *handler({ payload }, { put, call, select }) {
      const { url, data, type } = payload;
      yield promisify(confirm, {
        content: `您确定要${type}吗？`,
        okText: '确认',
        cancelText: '取消',
      });

      yield put.resolve({
        type: '$request/post',
        payload: { url, data },
      });

      yield put({
        type: '$msg/showToast',
        payload: {
          title: `${type}成功`,
          type: 'success',
        },
      });
    },
    *initDict({ payload }, { put, call, select }) {
      const res = yield put.resolve({
        type: '$request/post',
        payload: {
          url: '/api/data/v1/getDataList', // '/api/data/v1/getDataList',
          data: payload,
        },
      });
      yield put({
        type: 'save',
        payload: {
          dict: res,
        },
      });
    },

    *approveSetting({ payload }, { put, call, select }) {
      const setting = yield put.resolve({
        type: '$request/post',
        payload: {
          url: '/api/config/v1/getReportConfigItemList',
        },
      });
      const plan = setting.find(item => item.name.indexOf('规划') >= 0);
      const year = setting.find(item => item.name.indexOf('本年') >= 0);
      const nextYear = setting.find(item => item.name.indexOf('下年') >= 0);
      console.log('系统设置:', plan);
      let _approve = {
        plan: plan.value,
        year: year.value,
        nextYear: nextYear.value,
      };
      yield put({
        type: 'save',
        payload: {
          approve: _approve,
        },
      });
    },
  },
};
